1
|
|
|
/** |
2
|
|
|
* @author Piotr Mrowczynski <[email protected]> |
3
|
|
|
* |
4
|
|
|
* @copyright Copyright (c) 2017, Piotr Mrowczynski. |
5
|
|
|
* @license AGPL-3.0 |
6
|
|
|
* |
7
|
|
|
* This code is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
9
|
|
|
* as published by the Free Software Foundation. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* global: OCA, OC, _ |
24
|
|
|
* |
25
|
|
|
* */ |
26
|
|
|
|
27
|
|
|
var Files_PaperHive; |
28
|
|
|
Files_PaperHive = { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Holds the notification container |
32
|
|
|
*/ |
33
|
|
|
$notification: null, |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Holds the notification html |
37
|
|
|
*/ |
38
|
|
|
container: null, |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Holds the notification html options |
42
|
|
|
*/ |
43
|
|
|
containerOptions: null, |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets extensions |
47
|
|
|
*/ |
48
|
|
|
getExtension: function (fileName) { |
49
|
|
|
var parts = fileName.split('.'); |
50
|
|
|
var extension = ""; |
51
|
|
|
if (parts.length > 1) { |
52
|
|
|
extension = parts.pop(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return extension; |
56
|
|
|
}, |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets file name without extension |
60
|
|
|
*/ |
61
|
|
|
getFileNameWithoutExtension: function (fileName) { |
62
|
|
|
var filename = fileName; |
63
|
|
|
var parts = filename.split('.'); |
64
|
|
|
if (parts.length > 1) { |
65
|
|
|
filename = parts[0]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return filename; |
69
|
|
|
}, |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets if is paperhive file |
73
|
|
|
*/ |
74
|
|
|
isPaperHive: function (fileName) { |
75
|
|
|
return (OCA.Files_PaperHive.getExtension(fileName) === 'paperhive'); |
76
|
|
|
|
77
|
|
|
}, |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Registers the file actions |
81
|
|
|
*/ |
82
|
|
|
registerFileActions: function () { |
83
|
|
|
var mimetype = 'application/octet-stream'; |
84
|
|
|
|
85
|
|
|
OCA.Files.fileActions.registerAction({ |
86
|
|
|
name: 'ShowPaper', |
87
|
|
|
displayName: '', |
88
|
|
|
altText: t('core', 'Show Paper'), |
89
|
|
|
mime: mimetype, |
90
|
|
|
actionHandler: _.bind(this._onPaperHiveTrigger, this), |
|
|
|
|
91
|
|
|
permissions: OC.PERMISSION_READ, |
92
|
|
|
iconClass: 'icon-filetype-paperhive', |
93
|
|
|
type: OCA.Files.FileActions.TYPE_INLINE, |
94
|
|
|
render: function (actionSpec, isDefault, context) { |
95
|
|
|
if (OCA.Files_PaperHive.isPaperHive(context.$file.attr('data-file'))) { |
96
|
|
|
return OCA.Files.fileActions._defaultRenderAction.call(OCA.Files.fileActions, actionSpec, isDefault, context); |
97
|
|
|
} |
98
|
|
|
// don't render anything |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
}); |
102
|
|
|
}, |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Setup on page load |
106
|
|
|
*/ |
107
|
|
|
initialize: function () { |
108
|
|
|
$(document).bind('mouseup', this._onClickDocument); |
109
|
|
|
this.$notification = null; |
110
|
|
|
this.registerFileActions(); |
111
|
|
|
}, |
112
|
|
|
|
113
|
|
|
createContainer: function () { |
114
|
|
|
var self = this; |
115
|
|
|
$.ajax({ |
116
|
|
|
type: 'GET', |
117
|
|
|
url: OC.generateUrl('/apps/files_paperhive/ajax/getpaperhivedetails') |
118
|
|
|
}) |
119
|
|
|
.done(function (phdata) { |
120
|
|
|
var containerString = '<div class="icon-paperhive"></div>' + |
121
|
|
|
'<div><p class="normal">' + t('files_paperhive', 'Visit PaperHive at') + ' </p><p class="normal">' + phdata.paperhive_base_url + '</p><p class="normal"> ' + t('files_paperhive', 'and transform reading into a process of collaboration!') + '</p></div>' + |
122
|
|
|
//'<div><span></span></div>' + |
123
|
|
|
'<div><p class="bold">' + t('files_paperhive', 'Your DocID') + ' </p><p class="normal">' + t('files_paperhive', 'is the last fragment of PaperHive document URL.') + '</p></div>' + |
124
|
|
|
'<div><p class="normal">' + t('files_paperhive', 'Exemplary URL') + ': </p><p class="bold">' + phdata.paperhive_base_url + phdata.paperhive_document_url + 'Ra5WnkxImoOE</p></div>'; |
125
|
|
|
|
126
|
|
|
self.container = $('<div class="notification_paperhive"></div>').html( |
127
|
|
|
containerString |
128
|
|
|
); |
129
|
|
|
self.containerOptions = { |
130
|
|
|
isHTML: true, |
131
|
|
|
timeout: 30 |
132
|
|
|
}; |
133
|
|
|
self.$notification = OC.Notification.showHtml( |
134
|
|
|
self.container, |
135
|
|
|
self.containerOptions |
136
|
|
|
); |
137
|
|
|
}) |
138
|
|
|
.fail(function (message) { |
|
|
|
|
139
|
|
|
OC.dialogs.alert(t('files_paperhive', 'An error occurred!')); |
140
|
|
|
}); |
141
|
|
|
}, |
142
|
|
|
|
143
|
|
|
createNotification: function () { |
144
|
|
|
if (this.$notification === null) { |
145
|
|
|
this.createContainer(); |
146
|
|
|
} |
147
|
|
|
}, |
148
|
|
|
|
149
|
|
|
hideNotification: function () { |
150
|
|
|
if (!OC.Notification.isHidden() && this.$notification != null) { |
|
|
|
|
151
|
|
|
OC.Notification.hide(this.$notification); |
152
|
|
|
this.$notification = null; |
153
|
|
|
} |
154
|
|
|
}, |
155
|
|
|
|
156
|
|
|
failureNotification: function (message) { |
157
|
|
|
OC.dialogs.alert(message, t('files_paperhive', 'An error occurred!')); |
158
|
|
|
}, |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Loads the data through AJAX |
162
|
|
|
*/ |
163
|
|
|
loadMetadata: function (dir, filename, fetchDiscussions, success, failure) { |
164
|
|
|
$.get( |
165
|
|
|
OC.generateUrl('/apps/files_paperhive/ajax/loadmetadata'), |
166
|
|
|
{ |
167
|
|
|
filename: filename, |
168
|
|
|
dir: dir, |
169
|
|
|
fetchDiscussions: fetchDiscussions |
170
|
|
|
} |
171
|
|
|
).done(function (fileContents) { |
172
|
|
|
// Call success callback |
173
|
|
|
success(fileContents); |
174
|
|
|
}).fail(function (jqXHR) { |
175
|
|
|
var message; |
176
|
|
|
|
177
|
|
|
try { |
178
|
|
|
message = JSON.parse(jqXHR.responseText).message; |
179
|
|
|
} catch (e) { |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
failure(message); |
183
|
|
|
}); |
184
|
|
|
}, |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Handles request for book contents to PaperHive API |
188
|
|
|
*/ |
189
|
|
|
generatePaperHiveDocument: function (dir, bookID, success, failure) { |
190
|
|
|
$.get( |
191
|
|
|
OC.generateUrl('/apps/files_paperhive/ajax/generatePaperHiveDocument'), |
192
|
|
|
{ |
193
|
|
|
dir: dir, |
194
|
|
|
bookID: bookID |
195
|
|
|
} |
196
|
|
|
).done(function (paperHiveData) { |
197
|
|
|
// Success - found valid Book at PaperHive |
198
|
|
|
success(paperHiveData); |
199
|
|
|
}).fail(function (jqXHR) { |
200
|
|
|
var message; |
201
|
|
|
|
202
|
|
|
try { |
203
|
|
|
message = JSON.parse(jqXHR.responseText).message; |
204
|
|
|
} catch (e) { |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
failure("Error occured while connecting to PaperHive: " + message); |
208
|
|
|
}); |
209
|
|
|
|
210
|
|
|
}, |
211
|
|
|
|
212
|
|
|
setDiscussionCount: function ($tr) { |
213
|
|
|
OCA.Files_PaperHive.loadMetadata( |
214
|
|
|
$tr.attr('data-path'), |
215
|
|
|
$tr.attr('data-file'), |
216
|
|
|
"true", |
217
|
|
|
function (paperHiveData) { |
218
|
|
|
var discussionsCount = paperHiveData.paperhive_discussion_count; |
219
|
|
|
OCA.Files_PaperHive._updatePaperHiveFileData($tr, discussionsCount); |
220
|
|
|
}, |
221
|
|
|
function (message) { |
|
|
|
|
222
|
|
|
} |
223
|
|
|
); |
224
|
|
|
}, |
225
|
|
|
|
226
|
|
|
_updatePaperHiveFileData: function ($tr, discussionCount) { |
227
|
|
|
$tr.find('.filename .thumbnail').css('background-image', 'url(' + OC.imagePath('files_paperhive', 'paperhive-icon') + ')'); |
228
|
|
|
var action = $tr.find('.fileactions .action[data-action="ShowPaper"]'); |
229
|
|
|
|
230
|
|
|
action.addClass('shared-style'); |
231
|
|
|
var icon = action.find('.icon'); |
232
|
|
|
|
233
|
|
|
var message = ''; |
234
|
|
|
if (discussionCount === -1) { |
235
|
|
|
message = t('files_paperhive', 'Discuss'); |
236
|
|
|
} else { |
237
|
|
|
message = t('files_paperhive', 'Discuss') + ' (' + discussionCount + ')'; |
238
|
|
|
} |
239
|
|
|
action.html('<span> ' + message + '</span>').prepend(icon); |
240
|
|
|
|
241
|
|
|
// Adjust filename and extension |
242
|
|
|
var fullFilename = $tr.find('.innernametext').html(); |
243
|
|
|
var revisionNumber = OCA.Files_PaperHive.getExtension(fullFilename); |
244
|
|
|
if (revisionNumber !== "") { |
245
|
|
|
// If there is revision number, adjust file name to be displayed |
246
|
|
|
$tr.find('.innernametext').html(OCA.Files_PaperHive.getFileNameWithoutExtension(fullFilename)); |
247
|
|
|
} |
248
|
|
|
}, |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Handles the FileAction click event |
252
|
|
|
*/ |
253
|
|
|
_onPaperHiveTrigger: function (filename, context) { |
254
|
|
|
// Get the file data |
255
|
|
|
this.loadMetadata( |
256
|
|
|
context.dir, |
257
|
|
|
filename, |
258
|
|
|
"false", |
259
|
|
|
function (paperHiveData) { |
260
|
|
|
var paperhiveUrl = paperHiveData.paperhive_base_url + paperHiveData.paperhive_document_url + paperHiveData.paperhive_document_id; |
261
|
|
|
|
262
|
|
|
OC.dialogs.confirm( |
263
|
|
|
t('files_paperhive', 'Would you like to open PaperHive Document in the new window?'), |
264
|
|
|
t('files_paperhive', 'Open PaperHive Document'), |
265
|
|
|
function(confirmation) { |
266
|
|
|
if (confirmation) { |
267
|
|
|
var w = window.open(paperhiveUrl, '_blank'); |
268
|
|
|
if (!w) { |
269
|
|
|
window.location.href = paperhiveUrl; |
270
|
|
|
} |
271
|
|
|
} else { |
272
|
|
|
window.location.href = paperhiveUrl; |
273
|
|
|
} |
274
|
|
|
}, |
275
|
|
|
true |
276
|
|
|
); |
277
|
|
|
}, |
278
|
|
|
function (message) { |
|
|
|
|
279
|
|
|
OC.dialogs.alert(t('files_paperhive', 'An error occurred!')); |
280
|
|
|
} |
281
|
|
|
); |
282
|
|
|
}, |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Handles event when clicking outside editor |
286
|
|
|
*/ |
287
|
|
|
_onClickDocument: function (event) { |
288
|
|
|
var menuItem = $(event.target).closest('.menuitem'); |
289
|
|
|
var notificationItem = $(event.target).closest('.notification_paperhive'); |
290
|
|
|
|
291
|
|
|
if (menuItem.length && menuItem.attr('data-action') === 'paperhive') { |
292
|
|
|
OCA.Files_PaperHive.createNotification(); |
293
|
|
|
} else if (!notificationItem.length) { |
294
|
|
|
OCA.Files_PaperHive.hideNotification(); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
}; |
298
|
|
|
|
299
|
|
|
Files_PaperHive.NewFileMenuPlugin = { |
300
|
|
|
|
301
|
|
|
attach: function (menu) { |
302
|
|
|
var fileList = menu.fileList; |
303
|
|
|
|
304
|
|
|
// only attach to main file list, public view is not supported yet |
305
|
|
|
if (fileList.id !== 'files') { |
306
|
|
|
return; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// register the new menu entry |
310
|
|
|
menu.addMenuEntry({ |
311
|
|
|
id: 'paperhive', |
312
|
|
|
displayName: t('files_paperhive', 'PaperHive'), |
313
|
|
|
templateName: t('files_paperhive', 'URL or DocID'), |
314
|
|
|
iconClass: 'icon-filetype-paperhive', |
315
|
|
|
fileType: 'file', |
316
|
|
|
actionHandler: function (bookURL) { |
317
|
|
|
// Hide any remaining notification |
318
|
|
|
OCA.Files_PaperHive.hideNotification(); |
319
|
|
|
|
320
|
|
|
var bookArray = bookURL.split( '/' ); |
321
|
|
|
var bookID = ''; |
322
|
|
|
if (bookArray.length === 1) { |
323
|
|
|
// User gave only ID |
324
|
|
|
bookID = bookArray[0]; |
325
|
|
|
} else if (bookArray.length > 0){ |
326
|
|
|
// User gave URL |
327
|
|
|
bookID = bookArray[bookArray.length-1]; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
// Get the file data from PaperHive API |
332
|
|
|
var dir = fileList.getCurrentDirectory(); |
333
|
|
|
var $saveNotification = OC.Notification.showHtml(t('files_paperhive', 'Saving...')); |
334
|
|
|
OCA.Files_PaperHive.generatePaperHiveDocument( |
335
|
|
|
dir, |
336
|
|
|
bookID, |
337
|
|
|
function (paperHiveData) { |
338
|
|
|
var filename = paperHiveData.filename+paperHiveData.extension; |
339
|
|
|
|
340
|
|
|
fileList.filesClient.getFileInfo( |
341
|
|
|
paperHiveData.path, |
342
|
|
|
{ |
343
|
|
|
properties: fileList._getWebdavProperties() |
344
|
|
|
}) |
345
|
|
|
.then(function(status, data) { |
346
|
|
|
fileList.add(data, {scrollTo: true}); |
347
|
|
|
var $tr = fileList.findFileEl(filename); |
348
|
|
|
OCA.Files_PaperHive._updatePaperHiveFileData($tr, paperHiveData.discussionCount); |
349
|
|
|
OC.Notification.hide($saveNotification); |
350
|
|
|
}) |
351
|
|
|
.fail(function(status) { |
|
|
|
|
352
|
|
|
OC.Notification.hide($saveNotification); |
353
|
|
|
OCA.Files_PaperHive.failureNotification(t('files_paperhive', 'Please reload the page, error occured')); |
354
|
|
|
}); |
355
|
|
|
}, |
356
|
|
|
function (message) { |
|
|
|
|
357
|
|
|
OC.Notification.hide($saveNotification); |
358
|
|
|
OCA.Files_PaperHive.failureNotification(t('files_paperhive', 'Cannot add your PaperHive document of ID {id}. File exists or error connecting to PaperHive', {id: bookID})); |
359
|
|
|
} |
360
|
|
|
); |
361
|
|
|
} |
362
|
|
|
}); |
363
|
|
|
} |
364
|
|
|
}; |
365
|
|
|
|
366
|
|
|
Files_PaperHive.FileMenuPlugin = { |
367
|
|
|
|
368
|
|
|
attach: function (fileList) { |
369
|
|
|
// use delegate to catch the case with multiple file lists |
370
|
|
|
fileList.$el.on('fileActionsReady', function (ev) { |
371
|
|
|
var $files = ev.$files; |
372
|
|
|
var $phfiles = []; |
373
|
|
|
|
374
|
|
|
_.each($files, function (file) { |
|
|
|
|
375
|
|
|
var $tr = $(file); |
376
|
|
|
|
377
|
|
|
if (OCA.Files_PaperHive.isPaperHive($tr.attr('data-file'))) { |
378
|
|
|
OCA.Files_PaperHive._updatePaperHiveFileData($tr, -1); |
379
|
|
|
$phfiles.push(file); |
380
|
|
|
} |
381
|
|
|
}); |
382
|
|
|
|
383
|
|
|
setTimeout(function () { |
384
|
|
|
_.each($phfiles, function (file) { |
|
|
|
|
385
|
|
|
var $tr = $(file); |
386
|
|
|
OCA.Files_PaperHive.setDiscussionCount($tr); |
387
|
|
|
}); |
388
|
|
|
}, 20); |
389
|
|
|
|
390
|
|
|
}); |
391
|
|
|
} |
392
|
|
|
}; |
393
|
|
|
|
394
|
|
|
OCA.Files_PaperHive = Files_PaperHive; |
395
|
|
|
|
396
|
|
|
OC.Plugins.register('OCA.Files.NewFileMenu', Files_PaperHive.NewFileMenuPlugin); |
397
|
|
|
OC.Plugins.register('OCA.Files.FileList', Files_PaperHive.FileMenuPlugin); |
398
|
|
|
|
399
|
|
|
$(document).ready(function () { |
400
|
|
|
OCA.Files_PaperHive.initialize(); |
401
|
|
|
}); |
402
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.